package wiki.xsx.jg.core;

import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Clob;
import java.util.*;

/**
 * Oracle实现类
 */
public class OracleService extends AbstractService{

    public OracleService(Database database) {
        super(database);
    }

    @Override
    public String getDropTableSQL(String className) {
        StringBuilder sql = new StringBuilder();
        sql.append("DECLARE ")
            .append("   num number; ")
            .append("BEGIN ")
            .append("    SELECT COUNT(1) INTO num FROM user_tables WHERE table_name = ('").append(className.toUpperCase()).append("'); ")
            .append("    IF num > 0 THEN ")
            .append("        EXECUTE IMMEDIATE 'DROP TABLE ").append(className.toUpperCase()).append("'; ")
            .append("    END IF; ")
            .append("END;");
        return sql.toString();
    }

    @Override
    public Class<?> getTypeClass(String dataTypeName, int length, int scale) {
        if (dataTypeName.matches("CHAR|VARCHAR2|LONG")){
            return String.class;
        }else if(dataTypeName.matches("NUMBER")) {
            if (scale==0){
                if (length==1){
                    return Short.class;
                }else if (2<=length&&length<=9){
                    return Integer.class;
                }else if (10<=length&&length<=18){
                    return Long.class;
                }else {
                    return BigDecimal.class;
                }
            }else if (scale==1){
                return Float.class;
            }else{
                return Double.class;
            }
        }else if(dataTypeName.matches("DATE|TIME|TIMESTAMP")) {
            return Date.class;
        }else if(dataTypeName.matches("BLOB")) {
            return Blob.class;
        }else if(dataTypeName.matches("CLOB")) {
            return Clob.class;
        }else{
            return Object.class;
        }
    }

    @Override
    public String getDataType(Class<?> type) {
        StringBuilder builder = new StringBuilder();
        switch (type.getSimpleName().toLowerCase()){
            case "string":
                builder.append("VARCHAR2(255)");
                break;
            case "integer":
                builder.append("NUMBER");
                break;
            case "long":
                builder.append("NUMBER");
                break;
            case "float":
                builder.append("NUMBER");
                break;
            case "double":
                builder.append("NUMBER");
                break;
            case "bigdecimal":
                builder.append("NUMBER");
                break;
            case "short":
                builder.append("NUMBER");
                break;
            case "boolean":
                builder.append("NUMBER");
                break;
            case "byte":
                builder.append("NUMBER");
                break;
            case "date":
                builder.append("Date");
                break;
            case "time":
                builder.append("Date");
                break;
            case "timestamp":
                builder.append("TIMESTAMP");
                break;
            case "blob":
                builder.append("BLOB");
                break;
            case "clob":
                builder.append("CLOB");
                break;
            case "byte[]":
                builder.append("RAW");
                break;
            default:
                builder.append("VARCHAR2(255)");
        }
        return builder.toString();
    }
}
